<style lang="less" scoped>
  .average-month{
    width: 100%;
    height: 100%;
    &_title{
      height: 40px;
      background: url('~@/images/contenttitle.png');
      background-repeat: no-repeat;
      background-position: bottom;
      text-align: center;
      color: #00F6FF;
      line-height: 40px;
      margin-bottom: 5px;
    }
    &_content{
      height: calc(100% - 45px);
    }
  }
</style>

<template>
  <div class="average-month">
    <div class="average-month_title">
      <span>月平均住院日</span>
    </div>
    <div id="averageMonth" class="average-month_content" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { getAvgDiagnosisHod, getStrokeAvgDiagnosisHod } from '@/api/comprehensive/averageMonth'
export default {
  props: {
    isStrock: {
      type: String,
      default: 'chestPain'
    }
  },
  data() {
    return {
      setInter: null
    }
  },
  watch: {
    'isStrock'() {
      this.init()
    }
  },
  mounted() {
    this.init()
    this.setInter = setInterval(() => {
      this.init()
    }, 5000)
  },
  destroyed() {
    clearInterval(this.setInter)
  },
  methods: {
    init() {
      let myChart = echarts.init(document.getElementById('averageMonth'))
      // myChart.clear()
      myChart.hideLoading()
      window.addEventListener('resize', myChart.resize)
      let STEMIData = []
      let NSTEMIData = []
      let UAData = []
      let AorticData = []
      let EmbolismData = []
      if (this.isStrock == 'chestPain') {
        getAvgDiagnosisHod({
          hospitalId: this.$store.getters.hospitalId
        }).then(res => {
          if (res.code == 0) {
            if (res.data && res.data.length > 0) {
              res.data.forEach(el => {
                if (el.diagnosisName == 'STEMI') {
                  STEMIData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
                if (el.diagnosisName == 'NSTEMI') {
                  NSTEMIData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
                if (el.diagnosisName == 'UA') {
                  UAData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
                if (el.diagnosisName == '主动脉夹层') {
                  AorticData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
                if (el.diagnosisName == '肺动脉栓塞') {
                  EmbolismData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
              })
              let option = {
                color: ['#F0657A', '#1CD1A1', '#59BCFB', '#F9D435', '#008CFF'],
                legend: {
                  type: 'plain',
                  top: 0,
                  left: '15%',
                  icon: 'circle',
                  textStyle: {
                    color: '#fff'
                  },
                  formatter: function(name) {
                    return name
                  }
                },
                grid: {
                  top: '20%',
                  letf: '10%',
                  bottom: '20%'
                },
                xAxis: {
                  type: 'category',
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  data: ['本月', '上月', '同期']
                },
                yAxis: [{
                  name: '（天）',
                  type: 'value',
                  nameTextStyle: {
                    color: '#fff'
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  }
                }],
                tooltip: {
                  trigger: 'axis',
                  textStyle: {
                    color: '#00F6FF'
                  },
                  axisPointer: {
                    type: 'cross',
                    crossStyle: {
                      color: '#999'
                    }
                  },
                  formatter: '{b}<br />{a0}: {c0}<br />{a1}: {c1}<br />{a2}: {c2}<br />{a3}: {c3}<br />{a4}: {c4}'
                },
                series: [
                  {
                    name: 'STEMI',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: STEMIData
                  },
                  {
                    name: 'NSTEMI',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: NSTEMIData
                  },
                  {
                    name: 'UA',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: UAData
                  },
                  {
                    name: '主动脉夹层',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: AorticData
                  },
                  {
                    name: '肺栓塞',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: EmbolismData
                  }
                ]
              }
              myChart.setOption(option)
            } else {
              myChart.showLoading({
                text: '暂无数据',
                fontSize: 18,
                left: 'center',
                color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
                textColor: '#fff', // 文字颜色
                maskColor: 'rgba(255, 255, 255, 0)' // 背景色
              })
            }
          } else {
            myChart.showLoading({
              text: '暂无数据',
              fontSize: 18,
              left: 'center',
              color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
              textColor: '#fff', // 文字颜色
              maskColor: 'rgba(255, 255, 255, 0)' // 背景色
            })
          }
        })
      } else if (this.isStrock == 'stroke') {
        getStrokeAvgDiagnosisHod({
          hospitalId: this.$store.getters.hospitalId
        }).then(res => {
          if (res.code == 0) {
            if (res.data && res.data.length > 0) {
              res.data.forEach(el => {
                if (el.diagnosisName == '脑梗死') {
                  STEMIData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
                if (el.diagnosisName == '脑出血') {
                  NSTEMIData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
                if (el.diagnosisName == '颅内动脉瘤') {
                  UAData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
                if (el.diagnosisName == '颈动脉狭窄或闭塞') {
                  AorticData = [el.avgHodCurrentMonth, el.avgHodLastMonth, el.avgHodLastYear]
                }
              })
              let option = {
                color: ['#F0657A', '#1CD1A1', '#59BCFB', '#F9D435', '#008CFF'],
                legend: {
                  type: 'plain',
                  top: 0,
                  left: '15%',
                  icon: 'circle',
                  textStyle: {
                    color: '#fff'
                  },
                  formatter: function(name) {
                    return name
                  }
                },
                grid: {
                  top: '20%',
                  letf: '10%',
                  bottom: '20%'
                },
                xAxis: {
                  type: 'category',
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  data: ['本月', '上月', '同期']
                },
                yAxis: [{
                  name: '（天）',
                  type: 'value',
                  nameTextStyle: {
                    color: '#fff'
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  }
                }],
                tooltip: {
                  trigger: 'axis',
                  textStyle: {
                    color: '#00F6FF'
                  },
                  axisPointer: {
                    type: 'cross',
                    crossStyle: {
                      color: '#999'
                    }
                  },
                  formatter: '{b}<br />{a0}: {c0}<br />{a1}: {c1}<br />{a2}: {c2}<br />{a3}: {c3}'
                },
                series: [
                  {
                    name: '脑梗死',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: STEMIData
                  },
                  {
                    name: '脑出血',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: NSTEMIData
                  },
                  {
                    name: '动脉瘤',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: UAData
                  },
                  {
                    name: '劲动脉狭窄或闭塞',
                    type: 'line',
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: AorticData
                  }
                ]
              }
              myChart.setOption(option)
            } else {
              myChart.showLoading({
                text: '暂无数据',
                fontSize: 18,
                left: 'center',
                color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
                textColor: '#fff', // 文字颜色
                maskColor: 'rgba(255, 255, 255, 0)' // 背景色
              })
            }
          }
        })
      }
    }
  }
}
</script>
